package zhang.hao.jia.utils.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * 时间工具类
 */
public class DateUtil {

    public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");

    public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    public static Long parseLong(LocalDateTime time) {
        if (time == null) {
            return null;
        }
        return time.atZone(ZONE_ID).toInstant().toEpochMilli();
    }

    public static LocalDateTime parseLocalDateTime(Long time) {
        if (time == null) {
            return null;
        }
        return Instant.ofEpochMilli(time).atZone(ZONE_ID).toLocalDateTime();
    }

    public static LocalDateTime nowAsiaShanghai() {
        return LocalDateTime.now(ZONE_ID);
    }

    private DateUtil(){
    }
}
